﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TecBaseObjects.Objects;
using TecExtensions;
using TpNotiz.Controller;
using TpNotiz.Interfaces;

namespace TpNotiz.Forms {
    public partial class TecNotiz : Form, ITecForm {

        #region Members

        private TecNotizObj myModel;
        private Organisation myOrganisation;
        private TecNotizController controller;
        private Extensions.TelType anrufType;
        private int myStatus;

        #endregion

        #region Constructor

        public TecNotiz(TecNotizObj myModel) {
            this.controller = new TecNotizController(this);
            InitializeComponent();
            this.MyModel = myModel;
            this.controller.LoadModels();
            SetFormDataSources();
            SetControls();
            this.controller.SetControlValues();
        }

        #endregion

        #region Properties
        
        public TecNotizObj MyModel {
            get { return this.myModel; }
            set { this.myModel = value; }
        }

        public Organisation MyOrgModel {
            get { return this.myOrganisation; }
            set { this.myOrganisation = value; }
        }

        public List<TecNotizStatus> MyNotizStatusList {
            get;
            set;
        }

        public List<TecNotizTelefonType> MyNotizTelefonTypes {
            get;
            set;
        }

        public List<TecNotizTargetUser> MyTargetUserModel {
            get;
            set;
        }

        public Extensions.TelType AnrufType {
            get { return this.anrufType; }
            set { this.anrufType = value; }
        }

        public int MyStatus {
            get { return this.myStatus; }
            set { this.myStatus = value; }
        }

        #endregion

        #region Methods

        private void CloseForm(object sender, EventArgs e) {
            this.controller.Close(sender, e);
        }

        private void Save(object sender, EventArgs e) {
            this.myModel.Status = (int)this.cboStatus.SelectedValue;
            this.myModel.AnrufType = (Extensions.TelType)this.cboAnrufTyp.SelectedValue;
            this.controller.Save(this.MyModel);
        }

        private void SetCheckedChanged(object sender, EventArgs e) {
            this.controller.SetControlsTextChanged(sender, e);
        }

        private void SetControls() {

            this.cboAnrufTyp.SelectedValue = "AnrufTypeID";
            this.cboAnrufTyp.DisplayMember = "Typ";
            int selectedTypIndex = 0;
            foreach (TecNotizTelefonType ttt in MyNotizTelefonTypes) {
                if (ttt.TelTypeID == (int)MyModel.AnrufType) {
                    this.cboAnrufTyp.SelectedIndex = selectedTypIndex;
                    break;
                }
                selectedTypIndex++;
            }

            this.cboStatus.SelectedValue = "StatusID";
            this.cboStatus.DisplayMember = "StatusText";
            
            int selectedStatusIndex = 0;
            foreach (TecNotizStatus st in MyNotizStatusList) {
                if (st.StatusID == MyModel.Status) {
                    this.cboStatus.SelectedIndex = selectedStatusIndex;
                    break;
                }
                selectedStatusIndex++;
            }

        }

        private void SetFormDataSources() {
            this.bindingSourceNotiz.DataSource = MyModel;
            this.bindingSourceOrganisation.DataSource = MyOrgModel;
            this.bindingSourceAnrufTyp.DataSource = MyNotizTelefonTypes;
            this.bindingSourceAnrufStatus.DataSource = MyNotizStatusList;
            this.bindingSourceTargetUser.DataSource = MyTargetUserModel;
        }

        private void SetTextChanged(object sender, EventArgs e) {
            this.controller.SetControlsTextChanged(sender, e);
        }

        #endregion

        #region FormEvents
        
        private void dataGridView2_CellValidated(object sender, DataGridViewCellEventArgs e) {
            DataGridView dc = sender as DataGridView;
            if (dc.IsCurrentRowDirty) {
                this.MyModel.IsDirty = true;
            }
        }
        
        private void TecNotiz_FormClosing(object sender, FormClosingEventArgs e) {
            this.controller.FormClose(sender, e);
        }

        #endregion

        


    }
}
